# 时间调整 | 骑手拒单 | 游走 |
# 一天12小时
import random
import sys

sys.path.append("./")

from cognitive import rider_reflect

ONE_DAY = 360
ONE_HOUR = 30
ONE_MIN = 2


def plan_position(rider_info: dict):
    """
    计划第二天前往的地点
    """
    sensibility = rider_info["sensibility"]
    rationality = rider_info["rationality"]
    dic_ans = {}
    max_dic = rider_info["max_dic"]
    least_dic = rider_info["least_dic"]
    if len(max_dic) == 1:
        return

    for key in max_dic:
        ans = (max_dic[key] / (0.001 + max_dic["sum"]) * (rationality / 10) + least_dic[key] /
               (max_dic["sum"] * 0.001) * (sensibility / 10))
        dic_ans[key] = ans
    dic_values = sorted(dic_ans.values())
    max_value = dic_values[-2]
    print("dic_ans", dic_ans)
    for key, value in dic_ans.items():
        if value == max_value:
            input_ans = key[1:-1].split(',')
            int_list = list(map(int, input_ans))
            rider_info["start_position"] = int_list


def plan_time(rider_info):
    #  更新工作时间，改变：第二天工作完成后的health, dissatisfied, up_time, sleep_time
    #  health 要保留2个小时
    up_time = trick_to_time(rider_info["up_time"])[0]
    sleep_time = rider_info["sleep_time"]
    #  更新体力值, 这是在规划之前的身体状态
    change_num = int(rider_info["up_time"] - rider_info["sleep_time"] / ONE_HOUR)
    rationality = rider_info["rationality"]
    sensibility = rider_info["sensibility"]
    rider_status = rider_info["rider_status"]

    if rider_status == 'down':
        time_choose = probability_random([0, 1], [rationality / 10, sensibility / 10])
        if time_choose == 0:
            #  休息时间为7, 其中两个小时用来通勤、交流等事情，剩余5小时用来睡觉恢复体力
            rider_info["up_time"] = sleep_time + 7 * ONE_HOUR
            rider_info["sleep_time"] = rider_info["up_time"] + 5 * ONE_HOUR
        else:
            rider_info["up_time"] = sleep_time + 8 * ONE_HOUR
            rider_info["sleep_time"] = rider_info["up_time"] + 5 * ONE_HOUR
            health_change(rider_info, rider_info["health"], 1)
            print("222222222222222222222222222")
    else:
        time_choose = rider_info["dissatisfied"]
        if time_choose == 0:
            rider_info["up_time"] = sleep_time + 7 * ONE_HOUR
            rider_info["sleep_time"] = rider_info["up_time"] + 5 * ONE_HOUR
            print("vvvvvvvvvvvvvvvv")
        else:
            rider_reflect.get_richer_time(rider_info)  # 身体状态是良好，不满值大于1， 体力值是完成今日工作的状态


def check_money(rider_info: dict, new_money):
    #  更新金钱获得情况
    if rider_info["one_day_money"] >= new_money and rider_info["dissatisfied"] <= 10:
        rider_info["dissatisfied"] += 1
    elif rider_info["one_day_money"] < new_money:
        rider_info["dissatisfied"] -= 1
    rider_info["money"] += new_money
    rider_info["one_day_money"] = new_money
    for key in rider_info["new_time_money"].keys():
        rider_info["new_time_money"][key] = 0


def is_richer(rider_info, new_money):
    #  更新不满值
    if rider_info["one_day_money"] < new_money:
        rider_info["dissatisfied"] -= 1
        if rider_info["dissatisfied"] < 0:
            rider_info["dissatisfied"] = 0
    else:
        rider_info["dissatisfied"] += 1
        if rider_info["dissatisfied"] > 10:
            rider_info["dissatisfied"] = 10


def health_change(rider_info, num, add_num):
    """
    更新体力值和身体状态
    :param rider_info: 旗手信息
    :param num: 当前体力值
    :param add_num: 休息的时间
    :return:
    """
    num += add_num
    if num > 12: num = 12
    rider_info["health"] = num
    if num >= 5:
        rider_info["rider_status"] = "up"
    else:
        rider_info["rider_status"] = "down"


def probability_random(seq, probability):
    x = random.uniform(0, 1)
    cal_pro = 0.0
    ans = seq[0]
    for item, item_pro in zip(seq, probability):
        ans = item
        cal_pro += item_pro
        if x < cal_pro:
            break
    return ans


def trick_to_time(trick_step):
    """
    360模拟一天，一个trick表示2分钟
    :param trick_step: 时间步
    :return: [hour, min]
    """
    #  360模拟一天，一个trick表示2分钟
    trick_step %= ONE_DAY
    time_hour = int(trick_step / 30)
    time_min = trick_step % 30 * 2
    return [time_hour, time_min]


def time_to_trick(time_step):
    """
    转为trick
    :param time_step: [hour, min]
    :return:
    """
    time_hour = time_step[0]
    time_min = time_step[1]
    trick_step = time_hour * 30 + time_min * 2
    return trick_step


def plan_random_walk():
    pass
